package online_store.model.bus;

import java.sql.Connection;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import online_store.entity.UserEntity;
import online_store.model.dao.UserDao;
import online_store.model.dto.UserDTO;
import online_store.utils.Utils;

@Service
public class UserBus {
	
	@Autowired
	private UserDao userDao;
	
	@Transactional(value="jpaTransactionManager")
	public UserDTO getUser(int id){
		return convertEn2Dto(userDao.getUser(id));
	}
	
	@Transactional(value="jpaTransactionManager")
	public UserDTO findByUsername(String username){
		return convertEn2Dto(userDao.findByUsername(username));
	}
	
	@Transactional(value="jpaTransactionManager")
	public UserDTO findByUser_Pass(String username, String password){
		UserDTO user = convertEn2Dto(userDao.findByUsername(username));
		if(user == null){
			return null;
		}else{
			if(!user.getPassword().equals(Utils.getMD5(password, user.getSalt()))){
				return null;
			}
		}
		return user;
	}
	
	/*
	 * -1: Duplicated username
	 */
	@Transactional(value="jpaTransactionManager")
	public int createUser(UserDTO user){
		//validate duplicated user
		UserDTO existedUser = findByUsername(user.getUsername());
		if(existedUser != null){
			return -1;
		}
		
		//create salt
		UUID uuid = UUID.randomUUID();
		String salt = uuid.toString().substring(0, 10);
		
		String hashPass = Utils.getMD5(user.getPassword(), salt);
		user.setPassword(hashPass);
		user.setSalt(salt);
		
		userDao.createUser(convertDto2En(user));
		
		return 1;
	}
	
	/*
	 * -1: Duplicated username
	 * -2: Current password is invalid
	 */
	@Transactional(value="jpaTransactionManager")
	public int updateUser(int id, UserDTO userDTO){
		UserEntity user = userDao.getUser(id);
		
		//validate duplicated username
		if(!user.getUsername().equals(userDTO.getUsername())){
			if(userDao.findByUsername(userDTO.getUsername()) != null){
				return -1;
			}
		}
		
		//available changing pass, validate current password
		if(userDTO.getOldPass() != null){
			if(!user.getPassword().equals(Utils.getMD5(userDTO.getOldPass(), user.getSalt()))){
				return -2;
			}
			user.setPassword(userDTO.getNewPass());
		}
		//set entity
		user.setUsername(userDTO.getUsername());
		user.setEmail(userDTO.getEmail());
		
		userDao.updateUser(user);
		
		return 1;
	}
	
	private UserEntity convertDto2En(UserDTO user){
		if(user == null){
			return null;
		}
		UserEntity en = new UserEntity();
		en.setId(user.getId());
		en.setUsername(user.getUsername());
		en.setPassword(user.getPassword());
		en.setEmail(user.getEmail());
		en.setSalt(user.getSalt());
		return en;
	}
	
	private UserDTO convertEn2Dto(UserEntity en){
		if(en == null){
			return null;
		}
		UserDTO user = new UserDTO();
		user.setId(en.getId());
		user.setUsername(en.getUsername());
		user.setPassword(en.getPassword());
		user.setEmail(en.getEmail());
		user.setSalt(en.getSalt());
		return user;
	}
	
	
}







